package com.amiu.spring.config;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.util.Log4jConfigListener;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[]{MvcConfig.class,MybatisConfig.class,ShiroConfig.class};
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return null;
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}
	
	/**
	 * 这个方法tomcat启动就会调用,可以代替web.xml注册filter,listener,servlet等
	 */
	@Override
	public void onStartup(ServletContext container) throws ServletException {
		//config log4j listentr
		container.setInitParameter("log4jConfigLocation", "classpath:log4j.properties");
		container.addListener(Log4jConfigListener.class);
		
		//config shiro filter
		Dynamic filterRegistration = container.addFilter("shiroFilter", DelegatingFilterProxy.class);
		filterRegistration.setInitParameter("targetFilterLifecycle", "true");
		filterRegistration.addMappingForUrlPatterns(
			EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD), 
			false, "/*");//配置mapping
		
		super.onStartup(container);
	}
	
	
	
}
